/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.panel;

import org.rails.extjs.Option;
import org.rails.extjs.form.LoadAction;
import org.rails.extjs.form.SubmitAction;
import org.rails.extjs.javascript.JavaScript;
import org.rails.extjs.layout.FormLayout;
import org.rails.extjs.layout.Layout;

public class FormPanel extends Panel {
	
	public static final String LABEL_ALIGN_LEFT = "left";
	public static final String LABEL_ALIGN_RIGHT = "right";
	public static final String LABEL_ALIGN_TOP = "top";

	public FormPanel() {
		super();
		setFormId(createId());
	}

	public FormPanel(Option configOptions) {
		super(configOptions);
		setFormId(createId());
	}

	public FormPanel(String title) {
		super(title);
		setFormId(createId());
	}

	public FormPanel(String id, String title) {
		super(id, title);
		setFormId(createId());
	}

	public void setFormId(String formId){
		addOption("formId",formId);
	}
	
	public String getFormId(){
		return getString("formId","");
	}
	
	public void setHideLabels(Boolean hideLabels){
		addOption("hideLabels",hideLabels);
	}
	
	public Boolean isHideLabels(){
		return getBoolean("hideLabels",false);
	}
	
	public void setLabelAlign(String labelAlign){
		addOption("labelAlign",labelAlign);
	}
	
	public String getLabelAlign(){
		return getString("labelAlign",LABEL_ALIGN_LEFT);
	}
	
	public void setLabelPad(Integer labelPad){
		addOption("labelPad",labelPad);
	}
	
	public Integer getLabelPad(){
		return getInteger("labelPad",5);
	}
	
	public void setLabelWidth(Integer labelWidth){
		addOption("labelWidth",labelWidth);
	}
	
	public Integer getLabelWidth(){
		return getInteger("labelWidth",100);
	}
	
	public Layout getLayout(){
		return new FormLayout();
	}
	
	public void setMonitorPoll(Integer monitorPoll){
		addOption("monitorPoll",monitorPoll);
	}
	
	public Integer getMonitorPoll(){
		return getInteger("monitorPoll",200);
	}
	
	public void setMonitorValid(Boolean monitorValid){
		addOption("monitorValid",monitorValid);
	}
	
	public Boolean isMonitorValid(){
		return getBoolean("monitorValid",false);
	}
	
	public JavaScript load(LoadAction load){
		JavaScript js = new JavaScript();
		js.append("Ext.getCmp(\""+ getId() + "\")");
		js.append(".getForm().load(");
		js.append(load.getScript());
		js.append(");");
		return js;
	}
	
	public JavaScript submit(SubmitAction submit){
		JavaScript js = new JavaScript();
		js.append("Ext.getCmp(\""+ getId() + "\")");
		js.append(".getForm().submit(");
		js.append(submit.getScript());
		js.append(");");
		return js;
	}
	
	public void setFileUpload(Boolean fileUpload){
		addOption("fileUpload", fileUpload);
	}
	
	public Boolean isFileUpload(){
		return getBoolean("fileUpload",false);
	}
	
	@Override
	public String getScript() {
		script = new StringBuffer("");
		createItems();
		script.append("new Ext.FormPanel(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}
	
	
}
